//
//  RoadSlideV.m
//  TT
//
//  Created by Lenny on 16/4/18.
//  Copyright © 2016年 Lenny. All rights reserved.
//

#import "RoadSlideV.h"

#import "SidleModel.h"

@interface RoadSlideV ()
{
    UIImage *_cycleI;
    NSMutableString *_sr;
}
@property(nonatomic,strong)UIView *backLineV;
@property(nonatomic,strong)UIView *currLineV;
@property(nonatomic,strong)UIImageView *leftIV;
@property(nonatomic,strong)UIImageView *rightIV;
@property(nonatomic,strong)UILabel *leftL;
@property(nonatomic,strong)UILabel *rightL;
@property(nonatomic,strong)SidleModel *model;

@end

@implementation RoadSlideV

#pragma mark - life cycle 

- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    self.rightL.center = CGPointMake(self.rightIV.center.x, self.self.rightIV.center.y - 15);
    self.leftL.center = CGPointMake(self.leftIV.center.x, self.self.leftIV.center.y - 15);
    
}

#pragma mark - public motheds

- (void)addUI {

    _sr = [NSMutableString string];
    CGFloat between = 20;

    _cycleI = [UIImage imageNamed:@"icon_slide_cycle"];
    
    [self addSubview:self.backLineV];
    [self addSubview:self.startLabel];
    [self addSubview:self.endLabel];
    [self addSubview:self.currLineV];
    [self addSubview:self.leftIV];
    [self addSubview:self.rightIV];
    [self addSubview:self.leftL];
    [self addSubview:self.rightL];
    
    
    __weak typeof(self) weakSelf = self;
    [self.startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.left.mas_equalTo(between);
        make.centerY.equalTo(weakSelf);
    }];
    [self.endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-between);
        make.centerY.equalTo(weakSelf);
    }];
    
    [self.backLineV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(weakSelf);
        make.left.equalTo(weakSelf.startLabel.mas_right).offset( between /2);
        make.right.equalTo(weakSelf.endLabel.mas_left).offset(-between / 2);
        make.height.mas_equalTo(1);
    }];
    
    [self.currLineV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf);
        make.left.equalTo(weakSelf.backLineV);
        make.right.equalTo(weakSelf.backLineV);
        make.height.mas_equalTo(1);
    }];
    
    [self.leftIV mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.equalTo(weakSelf);
        make.left.equalTo(weakSelf.backLineV);
    }];
    
    [self.rightIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf);
        make.right.equalTo(weakSelf.backLineV);
    }];
}



#pragma mark - event motheds

- (void)swipeGesture:(UIPanGestureRecognizer *)gesture {
    
    CGFloat minX = CGRectGetMinX(self.backLineV.frame);
    CGFloat maxX = CGRectGetMaxX(self.backLineV.frame);
    
    CGPoint translation = [gesture translationInView:self];
    CGPoint toPoint = CGPointMake(gesture.view.center.x + translation.x,gesture.view.center.y);
    toPoint.x = toPoint.x < minX ? minX : toPoint.x;
    toPoint.x = toPoint.x > maxX ? maxX : toPoint.x;
    
    //移动文字和图标
    if (gesture.view == self.leftIV) {
        
        toPoint.x = toPoint.x < self.rightIV.center.x - _cycleI.size.width ? toPoint.x : self.rightIV.center.x - _cycleI.size.width;
        self.leftL.center = CGPointMake(toPoint.x, self.leftL.center.y);
        CGFloat rate = (toPoint.x - minX) /(maxX - minX);
        self.leftL.text = [self.model timeStrWithRate:rate];
        
   
        
    }else if (gesture.view == self.rightIV) {
        
        toPoint.x = toPoint.x > self.leftIV.center.x + _cycleI.size.width ? toPoint.x : self.leftIV.center.x + _cycleI.size.width;
        CGFloat rate = (toPoint.x - minX) /(maxX - minX);
        self.rightL.text = [self.model timeStrWithRate:rate];
        self.rightL.center = CGPointMake(toPoint.x, self.rightL.center.y);
        
    }
    gesture.view.center = toPoint;
    [gesture setTranslation:CGPointZero inView:self];
    
    //移动线
    CGFloat wide = self.rightIV.center.x - self.leftIV.center.x;
    CGFloat orignx = self.leftIV.center.x;
    self.currLineV.frame = CGRectMake(orignx, self.currLineV.frame.origin.y, wide, 1);
    
}

#pragma mark - setters and getters

- (UILabel *)startLabel {
    
    if (_startLabel == nil) {
        
        _startLabel = [[UILabel alloc] init];
        _startLabel.text = @"13:00";
    }
    return _startLabel;
}

- (UILabel *)endLabel {
    
    if (_endLabel == nil) {
        
        _endLabel = [[UILabel alloc] init];
        _endLabel.text = @"18:00";
    }
    return _endLabel;
}

- (UIView *)backLineV {
    
    if (_backLineV == nil) {
        
        _backLineV = [[UIView alloc] init];
        _backLineV.backgroundColor = WL_line_Color;
    }
    return _backLineV;
}

- (UIView *)currLineV {
    
    if (_currLineV == nil) {
        
        _currLineV = [[UIView alloc] init];
        _currLineV.backgroundColor = WL_Top_Color;
    }
    return _currLineV;
}

- (UIImageView *)leftIV {
    
    if (_leftIV == nil) {
        
        _leftIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _cycleI.size.width, _cycleI.size.height)];
        _leftIV.image = _cycleI;
        _leftIV.userInteractionEnabled = YES;
        
        UIPanGestureRecognizer *swipe = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        [_leftIV addGestureRecognizer:swipe];
    }
    return _leftIV;
}

- (UIImageView *)rightIV {
    
    if (_rightIV == nil) {
        
       
        _rightIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, _cycleI.size.width, _cycleI.size.height)];
        _rightIV.image = _cycleI;
        _rightIV.userInteractionEnabled = YES;
        
        UIPanGestureRecognizer *swipe = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGesture:)];
        [_rightIV addGestureRecognizer:swipe];
    }
    return _rightIV;
}

- (UILabel *)leftL {
    
    if (_leftL == nil) {
        
        _leftL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 35, 20)];
        _leftL.text = [self.model timeStrWithRate:0];
        _leftL.font = [UIFont systemFontOfSize:12];
        _leftL.textAlignment = NSTextAlignmentCenter;
        _leftL.textColor = WL_Desc_Color;
    }
    return _leftL;
}

- (UILabel *)rightL {
    
    if (_rightL == nil) {
        
        _rightL = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 35, 20)];
        _rightL.text = [self.model timeStrWithRate:1];
        _rightL.textColor = WL_Desc_Color;
        _rightL.font = [UIFont systemFontOfSize:12];
        _rightL.textAlignment = NSTextAlignmentCenter;
    }
    return _rightL;
}

- (SidleModel *)model {
    
    if (_model == nil) {
    
        _model = [[SidleModel alloc] init];
        _model.startDate = [NSDate dateWithTimeIntervalSinceNow:-10000];
        _model.endDate = [NSDate dateWithTimeIntervalSinceNow:1000];
        
        self.startLabel.text = [_model timeStrWithDate:_model.startDate];
        self.endLabel.text = [_model timeStrWithDate:_model.endDate];
    }
    return _model;
}

@end
